package com.study.view;

import com.study.bean.Telephone_new;
import com.study.service.impl.Telephone_newServiceImpl;
import com.study.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class Ui {
    public static void menu() {
        System.out.println("---------------========--通讯录---------=============--------");
        System.out.println("1.添加    2.删除    3.修改    4.查询所有  5.根据姓名查询    0.退出");
        System.out.println("---------========--------通讯录-------=============----------");
        System.out.println("请选择功能：");
    }


    public static void main(String[] args) throws SQLException {
        Connection c1 = JdbcUtil.getConnection();
        Telephone_newServiceImpl telSer = new Telephone_newServiceImpl();

        Scanner sc = new Scanner(System.in);
        while (true) {
            menu();
            int z = sc.nextInt();
            switch (z) {
                case 1:          //添加
                    System.out.println("-------添加通讯录-------");
                    System.out.print("姓名：");
                    String name = sc.next();
                    System.out.print("性别：");
                    String sex = sc.next();
                    System.out.print("年龄：");
                    int age = sc.nextInt();
                    System.out.print("电话：");
                    String tel = sc.next();
                    System.out.print("QQ：");
                    String qq = sc.next();
                    System.out.print("地址：");
                    String addr = sc.next();
                    Telephone_new item = new Telephone_new(null,name, sex, age, tel, qq, addr);
                    if (telSer.add(item)==1) {
                        System.out.println("添加成功");
                        System.out.println(item);
                    } else {
                        System.out.println("添加失败");
                    }
                    break;
                case 2: //删除
                    System.out.println("-------根据ID删除-------");
                    System.out.println("请输入Id");
                    Integer idDel = sc.nextInt();
                    if (telSer.selectById(idDel) == null) {
                        System.out.println("无法删除，该ID不存在");
                        break;
                    }
                    System.out.println("请确定是否删除"+"Y  或  N");
                    String result = sc.next();
                    if (result.equalsIgnoreCase("y")) {
                        telSer.del(idDel);
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    break;
                case 3://修改
                    System.out.println("-------修改通讯录-------");
                    System.out.print("id：");
                    Integer idold = sc.nextInt();

                    if (telSer.selectById(idold) == null) {
                        System.out.println("该ID不存在");
                        break;
                    }
                    System.out.print("新姓名：");
                    String namenew = sc.next();
                    System.out.print("新性别：");
                    String sexnew = sc.next();
                    System.out.print("新年龄：");
                    int agenew = sc.nextInt();
                    System.out.print("新电话：");
                    String telnew = sc.next();
                    System.out.print("新QQ：");
                    String qqnew = sc.next();
                    System.out.print("新地址：");
                    String addrnew = sc.next();
                    Telephone_new itemnew = new Telephone_new(idold,namenew, sexnew, agenew, telnew, qqnew, addrnew);
                    telSer.update(itemnew);
                    System.out.println("修改成功");
                    break;
                case 4://查询所有
                    System.out.println("-------查询所有通讯录-------");
                    List<Telephone_new> talArr = telSer.selectAll();
                    talArr.forEach(System.out::println);
                    break;
                case 5://根据姓名查询
                    System.out.println("-------根据姓名查询-------");
                    System.out.print("请输入姓名：");
                    String selectname = sc.next();

                    if (telSer.seletByName(selectname) != null) {
                        telSer.seletByName(selectname).forEach(System.out::println);

                    } else {
                        System.out.println("没有找到");
                    }
                    break;
                case 0://退出
                    System.exit(0);
                    break;
                default:
                    System.out.println("请重新输入：");

            }
        }
    }
}

